<template>
  <div class="app-container">
    医院设置添加
    <!-- 双向绑定， 将用户输入直接注入hospset -->
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospitalSet.hosname"/>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospitalSet.hoscode"/>
      </el-form-item>
      <el-form-item label="api基础路径">
        <el-input v-model="hospitalSet.apiUrl"/>
      </el-form-item>
      <el-form-item label="联系人姓名">
        <el-input v-model="hospitalSet.contactsName"/>
      </el-form-item>
      <el-form-item label="联系人手机">
        <el-input v-model="hospitalSet.contactsPhone"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import hospsetApi from '../../api/hospset'

export default {
  data() {
    return {
      hospitalSet: {}
    }
  },
  created() {
    // 获取路由id值 调用接口得到医院设置信息
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      console.log('跳转的id' + id)
      this.getHospitalSet(id)
    }
  },
  methods: {
    getHospitalSet(id) {
      hospsetApi.getHospitalSetById(id).then(
        res => {
          this.hospitalSet = res.data.data
        }
      )
    },
    save() {
      hospsetApi.saveHospitalSet(this.hospitalSet).then(
        result => {
          // 提示
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          //  跳转到医院设置列表
          this.$router.push({path: '/hospitalSet/list'})
        }
      )
    },
    update() {
      hospsetApi.updateHospitalSet(this.hospitalSet)
        .then(response => {
          // 提示
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          // 跳转列表页面，使用路由跳转方式实现
          this.$router.push({path: '/hospSet/list'})
        })
    },
    saveOrUpdate() {
      // 通过判断是否有id来判断是add还是update （无论用户输入什么信息，也不会有id）
      if (!this.hospitalSet.id) {
        // id不存在，save
        this.save()
      } else {
        this.update()
      }
    }
  }

}
</script>

